package com.pan.controller.LogAndRegisController;

import com.pan.aop.BusinessType;
import com.pan.aop.MyLog;
import com.pan.controller.tools.R1;
import com.pan.domain.User;
import com.pan.service.ILoginRegisterService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/register")
@Tag(name = "RegisterController",description = "用户注册")
public class RegisterController {
    @Autowired
    private ILoginRegisterService loginRegisterService;

    @Operation(summary = "注册",description = "会检查注册信息是否存在")
    @PostMapping
    @MyLog(title = "注册",optParam = "#{user}",businessType = BusinessType.OTHER)
    public R1 register(@Parameter(description = "登录的信息,为Json字符串",required = true) @RequestBody User user){
        System.out.println("----------------------------------------------------------------");
        System.out.println(user);
        System.out.println("----------------------------------------------------------------");
        //先判断用户名是否存在
        if(loginRegisterService.selectByUsername(user.getUsername())!=null){
            return new R1(false,"该用户已存在！");
        }
        //再判断成员名和邮箱是否存在
        if (loginRegisterService.selectByName(user.getName())!=null){
            return new R1(false,"成员名字重复！");
        }
        if (loginRegisterService.selectByEmail(user.getEmail())!=null){
            return new R1(false,"该邮箱已注册！");
        }
        //注册成功保存对象
        loginRegisterService.save(user);
        return new R1(true,"注册成功！");
    }
}
